﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using WmsApp.Service;
using WmsApp.Style;
using WmsApp.WindowControl;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormPayApplyBill : BaseForm
    {
        public FormPayApplyBill()
        {
            InitializeComponent();
        }

        public override string HeadText
        {
            get { return "付款申请单台账"; }
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            try
            {
                IEntityService<PayApplyBill, PayApplyBillService> service =
              EntityServiceFactory.GetEntityService<PayApplyBill, PayApplyBillService>();
                this.gridControlEdit1.DataSource = service.GetEntityList(this.qPayApplyBill1.GetCondition());
                RowStyleConditionController.AddStyleFormatCondition(this.gridView1, "PayApplyStateDesc", "[PayApplyStateDesc] != '确认'");
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdNew_Click(object sender, EventArgs e)
        {
            try
            {
                this.payApplyBillCtlMain.FPayApplyBillController.NewPayApplyBill(String.Empty);
                this.xtraTabControlMain.SelectedTabPage = this.xtraTabPage3;
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdEdit1_Click(object sender, EventArgs e)
        {
            var obj = this.gridView1.GetFocusedRow();
            if (obj != null)
            {
                try
                {
                    this.payApplyBillCtlMain.FPayApplyBillController.LoadPayApplyBillID(((PayApplyBill)obj).PayApplyBillID);
                    this.xtraTabControlMain.SelectedTabPage = this.xtraTabPage3;
                }
                catch (Exception ex)
                {
                    Exceptions.ExceptionHandler.ShowException(ex);
                }
            }
          
        }

        private void CmdQuery2_Click(object sender, EventArgs e)
        {
            try
            {
                IEntityService<PayApplyBillDtl, PayApplyBillDtlService> service =
                  EntityServiceFactory.GetEntityService<PayApplyBillDtl, PayApplyBillDtlService>();
                this.gridControlEdit2.DataSource = service.GetEntityList(this.qPayApplyBillDtl1.GetCondition());
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdNew2_Click(object sender, EventArgs e)
        {
            try
            {
                this.payApplyBillCtlMain.FPayApplyBillController.NewPayApplyBill(string.Empty);
                this.xtraTabControlMain.SelectedTabPage = this.xtraTabPage3;
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdEdit2_Click(object sender, EventArgs e)
        {
            try
            {
                var obj = this.gridView2.GetFocusedRow();
                if (obj != null)
                {
                    this.payApplyBillCtlMain.FPayApplyBillController.LoadPayApplyBillID(((PayApplyBillDtl)obj).PayApplyBillID);
                    this.xtraTabControlMain.SelectedTabPage = this.xtraTabPage3;
                }
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdQuery4_Click(object sender, EventArgs e)
        {
            try
            {
                var client = Factory.WmsServiceFactory.GetServiceClient();
                gridControlPayItem.DataSource =
                    client.GetPayApplyBillItemsByCondition(this.qPayApplyBillItemMain.GetCondition());
                RowStyleConditionController.AddStyleFormatCondition(this.gridView3, "ItemState", "[ItemState] == '作废'");
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdEdit4_Click(object sender, EventArgs e)
        {
            var obj = this.gridView3.GetFocusedRow();
            if (obj != null)
            {
                try
                {
                    var client = Factory.WmsServiceFactory.GetServiceClient();
                    var entity = client.GetPayApplyBillItem(((PayApplyBillItem) obj).PayApplyBillItemID);
                    payApplyBillItemCtlView.BindingPayApplyBillItem(entity);
                    this.xtraTabControlMain.SelectedTabPage = this.xtraTabPage5;
                }
                catch (Exception ex)
                {
                    Exceptions.ExceptionHandler.ShowException(ex);
                }
            }
        }

        private void RepositoryItemButtonEditCancelPayButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var obj = this.gridView3.GetFocusedRow();
            if (obj != null)
            {
                try
                {
                    var client = Factory.WmsServiceFactory.GetServiceClient();
                    if(System.Windows.Forms.DialogResult.Yes == XtraMessageBox.Show("您确实要取消该付款单吗？","提问",MessageBoxButtons.YesNo,MessageBoxIcon.Question))
                    {
                        var currentObj = (PayApplyBillItem) obj;
                        client.CancelPayApplyBillItem(currentObj.PayApplyBillItemID);
                        //后台实际对象
                        var backStorageObj = client.GetPayApplyBillItem(currentObj.PayApplyBillItemID);
                        //赋值，刷新
                        currentObj.ItemState = backStorageObj.ItemState;
                        currentObj.CancelID = backStorageObj.CancelID;
                        currentObj.CancelItemDate = backStorageObj.CancelItemDate;
                        currentObj.CancelName = backStorageObj.CancelName;
                        this.gridControlPayItem.RefreshDataSource();
                    }
                }
                catch (Exception ex)
                {
                    Exceptions.ExceptionHandler.ShowException(ex);
                }
            }
        }

        private void CmdPrint_Click(object sender, EventArgs e)
        {

        }
    }
}